﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Engine.Sgml.Schema;
using System.IO;
using Engine.Sgml.Validation;

namespace Engine.Sgml.Test.Util {
    /// <summary>
    /// Summary description for DtdSchemaGeneratorFixture
    /// </summary>
    [TestClass]
    public class GeneratorFixture {
        public GeneratorFixture() {
            //
            // TODO: Add constructor logic here
            //
            }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
                }
            set {
                testContextInstance = value;
                }
            }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void GenerateModelGroupsTest() {
            DElementConfiguration elementConfig = new DElementConfiguration();
            DModelGroupsConfiguration groupsConfig = new DModelGroupsConfiguration(elementConfig);
            DModelGroupsGenerator generator = new DModelGroupsGenerator();

            Random random = new Random();
            int count = 100000;
            do {
                groupsConfig.MaxLength = random.Next(1, 7);
                groupsConfig.MaxDepth = random.Next(1, 7);
                elementConfig.ElementNumber = 1;
                DModelGroupCollection model = generator.Generate(groupsConfig);
                string view = model.ToString();
                count--;
                } while (count > 0);
            }

        [TestMethod]
        public void GenerateDtdSchemesTest() {
            DElementConfiguration elementConfig = new DElementConfiguration();
            DModelGroupsConfiguration groupsConfig = new DModelGroupsConfiguration(elementConfig);
            DtdSchemaGenerator generator = new DtdSchemaGenerator();

            Random random = new Random();
            int count = 100000;
            do {
                groupsConfig.MaxLength = random.Next(1, 7);
                groupsConfig.MaxDepth = random.Next(1, 7);
                elementConfig.ElementNumber = 1;
                DtdSchema schema = generator.Generate(groupsConfig);
                count--;
                } while (count > 0);
            }

        [TestMethod]
        public void GenerateDtdSchemeWithInclusionElementsTest() {
            DElementConfiguration elementConfig = new DElementConfiguration();
            DModelGroupsConfiguration groupsConfig = new DModelGroupsConfiguration(elementConfig);
            groupsConfig.GenerateInclusionElements = true;
            DtdSchemaGenerator generator = new DtdSchemaGenerator();

            Random random = new Random();
            int count = 100000;
            do {
                groupsConfig.MaxLength = random.Next(1, 7);
                groupsConfig.MaxDepth = random.Next(1, 7);
                elementConfig.ElementNumber = 1;
                DtdSchema schema = generator.Generate(groupsConfig);
                count--;
                } while (count > 0);
            }

        [TestMethod]
        public void GenerateSgmlDocumentsTest() {
            DElementConfiguration elementConfig = new DElementConfiguration();
            DModelGroupsConfiguration groupsConfig = new DModelGroupsConfiguration(elementConfig);
            groupsConfig.GenerateInclusionElements = true;
            DtdSchemaGenerator schemaGenerator = new DtdSchemaGenerator();
            SgmlDocumentGenerator generator = new SgmlDocumentGenerator();

            Random random = new Random();
            int count = 100000;
            do {
                groupsConfig.MaxLength = random.Next(1, 7);
                groupsConfig.MaxDepth = random.Next(1, 7);
                elementConfig.ElementNumber = 1;

                DtdSchema schema = schemaGenerator.Generate(groupsConfig);
                SgmlDocument doc = generator.Generate(schema.RootDeclaration);
                string xml = doc.ToXml();

                count--;
                } while (count > 0);
            }

        [TestMethod]
        public void GenerateSgmlDocumentsWithCommentsTest() {
            DElementConfiguration elementConfig = new DElementConfiguration();
            DModelGroupsConfiguration groupsConfig = new DModelGroupsConfiguration(elementConfig);
            groupsConfig.GenerateInclusionElements = true;
            DtdSchemaGenerator schemaGenerator = new DtdSchemaGenerator();
            SgmlDocumentGenerator generator = new SgmlDocumentGenerator(true);

            Random random = new Random();
            int count = 100000;
            do {
                groupsConfig.MaxLength = random.Next(1, 7);
                groupsConfig.MaxDepth = random.Next(1, 7);
                elementConfig.ElementNumber = 1;

                DtdSchema schema = schemaGenerator.Generate(groupsConfig);
                SgmlDocument doc = generator.Generate(schema.RootDeclaration);
                string xml = doc.ToXml();

                count--;
                } while (count > 0);
            }

        private int MatchDepth(ModelGroupsMatch match) {
            ModelGroupsMatch parent = match.Parent;
            int depth = 0;
            while (parent != null) {
                parent = parent.Parent;
                depth++;
                }
            return depth;
            }

        [TestMethod]
        public void GenerateModelGroupsMatchTest() {
            ModelGroupsMatchGenerator generator = new ModelGroupsMatchGenerator();

            generator.Configuration.MaxDepth = 7;
            generator.Configuration.MaxInnerMatchCount = 7;
            generator.Configuration.MaxRepetitiveMatchCount = 7;
            generator.ModelGroupsConfiguration.MaxDepth = 1;
            int testCount = 1000;
            ModelGroupsMatchIterator matchIterator = null;

            do {
                generator.ModelGroupsElementConfiguration.ElementNumber = 1;
                ModelGroupsMatch match = generator.Generate();
                Assert.IsTrue(generator.Statistics.TotalMatchCount > 0);

                if (matchIterator == null)
                    matchIterator = new ModelGroupsMatchIterator(match);
                else
                    matchIterator.Reset(match);
                // Total
                int expectedTotalCount = 0;
                while (matchIterator.MoveNext()) {
                    expectedTotalCount++;
                    }
                Assert.IsTrue(generator.Statistics.TotalMatchCount == expectedTotalCount);
                // Inconsistent
                matchIterator.Reset(match);
                int expectedInconsistentMatchCount = 0;
                while (matchIterator.MoveNext()) {
                label:
                    if (matchIterator.Current.Type == AccordanceTypes.Inconsistent) {
                        expectedInconsistentMatchCount++;
                        if (matchIterator.Current.HasInner) {
                            int maxDepth = MatchDepth(matchIterator.Current);
                            do {
                                if (!matchIterator.MoveNext())
                                    goto finish;
                                if (MatchDepth(matchIterator.Current) <= maxDepth)
                                    break;
                                } while (matchIterator.Current.Type == AccordanceTypes.Inconsistent);
                            goto label;
                            }
                        }
                    }
            finish:
                Assert.IsTrue(generator.Statistics.TotalInconsistentMatchCount == expectedInconsistentMatchCount);
                // Incomplete
                matchIterator.Reset(match);
                int expectedIncompleteMatchCount = 0;
                while (matchIterator.MoveNext()) {
                    if (matchIterator.Current.Type == AccordanceTypes.Partial)
                        expectedIncompleteMatchCount++;
                    }
                Assert.IsTrue(generator.Statistics.TotalPartialAccordanceMatchCount == expectedIncompleteMatchCount);
                matchIterator.Reset(match);
                // Repetitive
                int expectedRepetitiveMatchCount = 0;
                while (matchIterator.MoveNext()) {
                    if (matchIterator.Current.Model.CanRecur())
                        expectedRepetitiveMatchCount++;
                    }

                Assert.IsTrue(generator.Statistics.TotalRepetitiveModelGroupsCount == expectedRepetitiveMatchCount);
                } while (--testCount > 0);

            }
        }
    }
